﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;


namespace KineBall
{
    public static class SoundManager
    {
        private static Dictionary<string, SoundEffect> soundEffects =
            new Dictionary<string, SoundEffect>();

        static Song currentSong;

        private static ContentManager content;

        private static string soundsDirectory;

        private static float soundEffectsVolume = 1.0f;

        public static void Initialize(Game game, string directory)
        {
            content = game.Content;

            soundsDirectory = directory;

            MediaPlayer.IsRepeating = true;
        }

        public static void PlaySound(string name)
        {
            SoundEffect effect;
            if (soundEffects.TryGetValue(name, out effect))
                effect.Play(soundEffectsVolume, 0.0f, 0.0f);
        }

        public static void PlayMusic(string name)
        {
            currentSong = null;

            try
            {
                currentSong = content.Load<Song>(soundsDirectory + "/" + name);
            }
            catch (Exception e)
            {
                if (currentSong == null)
                    throw e;
            }

            MediaPlayer.Play(currentSong);
        }

        public static void LoadSound(string assetName)
        {
            soundEffects.Add(assetName, content.Load<SoundEffect>(soundsDirectory + "/" + assetName));
        }

        public static void StopMusic()
        {
            MediaPlayer.Stop();
        }

        public static void SetSoundFXVolume(float volume)
        {
            soundEffectsVolume = volume;
        }

        public static void SetMusicVolume(float volume)
        {
            MediaPlayer.Volume = volume;
        }
    }
}